﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects;

namespace MINERP.DAL.InfoConfig
{
    public class UserDAL : DALBase
    {

        private static ObjectQuery<users> GetUser()
        {
            return DataEntites.users.Include("tb_Power").Include("tb_WorkerGroup").Where("it.IsUseful =true");
        }
        private static ObjectQuery<tb_Power> GetPower()
        {
            return DataEntites.tb_Power;
        }
        private static ObjectQuery<tb_WorkerGroup> GetGroup()
        {
            return DataEntites.tb_WorkerGroup;
        }
        private static ObjectQuery<tb_Action> GetAction()
        {
            return DataEntites.tb_Action.Include("tb_Power");
        }
        /// <summary>
        /// 获得单个用户及其操作权限
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static users GetUserAndAction(users obj)
        {
            return DataEntites.users.Include("tb_Action").First(u => u.userID == obj.userID);
        }
        /// <summary>
        /// 获得单个用户及其角色
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static users GetUserAndPower(users obj)
        {
            return DataEntites.users.Include("tb_Power").First(u => u.userID == obj.userID);
        }
        /// <summary>
        /// 获得单个角色的操作List
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static List<tb_Action> GetActionList(tb_Power obj)
        {
            List<tb_Action> list = new List<tb_Action>();
            ObjectQuery<tb_Action> actions = GetAction();
            foreach (var action in actions)
            {
                if (action.tb_Power.PowerId == obj.PowerId)
                    list.Add(action);
            }
            return list;
        }
        /// <summary>
        /// 获得所有用户list
        /// </summary>
        /// <returns></returns>
        public static List<users> GetUserList()
        {
            List<users> list = new List<users>();
            ObjectQuery<users> users = GetUser();
            try
            {
                foreach (users user in users)
                {
                    list.Add(user);
                }
            }
            catch (Exception e)
            {
            }
            return list;
        }
        /// <summary>
        /// 获得所有角色list
        /// </summary>
        /// <returns></returns>
        public static List<tb_Power> GetPowerList()
        {
            List<tb_Power> list = new List<tb_Power>();
            ObjectQuery<tb_Power> powers = GetPower();
            foreach (tb_Power power in powers)
            {
                list.Add(power);
            }
            return list;
        }
        /// <summary>
        /// 获得所有用户组list
        /// </summary>
        /// <returns></returns>
        public static List<tb_WorkerGroup> GetGroupList()
        {
            List<tb_WorkerGroup> list = new List<tb_WorkerGroup>();
            ObjectQuery<tb_WorkerGroup> groups = GetGroup();
            foreach (tb_WorkerGroup group in groups)
            {
                list.Add(group);
            }
            return list;
        }
        /// <summary>
        ///  获得单个角色对象
        /// </summary>
        /// <param name="powerName"></param>
        /// <returns></returns>
        public static tb_Power GetPowerObj(string powerName)
        {
            return DataEntites.tb_Power.First(p => p.Name == powerName);
        }
        /// <summary>
        ///  获得单个用户组对象
        /// </summary>
        /// <param name="powerName"></param>
        /// <returns></returns>
        public static tb_WorkerGroup GetGroupObj(string groupName)
        {
            return DataEntites.tb_WorkerGroup.First(w => w.GroupName == groupName);
        }
        /// <summary>
        /// 获得单个操作对象
        /// </summary>
        /// <param name="actionName"></param>
        /// <returns></returns>
        public static tb_Action GetActionObj(string actionName)
        {
            return DataEntites.tb_Action.Include("users").FirstOrDefault(a => a.ActionName == actionName);
        }

        /// <summary>
        /// 添加用户
        /// </summary>
        /// <param name="obj"></param>
        public static void AddUser(users obj)
        {
            DataEntites.AddTousers(obj);

            DataEntites.SaveChanges();
        }
        /// <summary>
        /// 更新用户
        /// </summary>
        /// <param name="obj"></param>
        public static int UpdateUser(users obj, List<tb_Action> actionList, List<tb_Tech> techList, List<tb_Power> powerList, string groupName)
        {
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                tb_WorkerGroup groupObj = db.tb_WorkerGroup.First(g => g.GroupName == groupName);
                users user = db.users.First(u => u.userID == obj.userID);
                var ut = db.users.FirstOrDefault(u => u.userID != obj.userID && u.UserName == obj.UserName&&u.IsUseful);
                if (ut != null)
                {
                    return -1;
                }

                if (!user.tb_Action.IsLoaded)
                {
                    user.tb_Action.Load();
                }
                user.tb_Action.Clear();
                foreach (tb_Action action in actionList)
                {
                    tb_Action act = db.tb_Action.FirstOrDefault(a=>a.ActionId ==action.ActionId);
                    user.tb_Action.Add(act);
                }
                if (!user.tb_Tech.IsLoaded)
                {
                    user.tb_Tech.Load();
                }
                user.tb_Tech.Clear();
                foreach (var item in techList)
                {
                    tb_Tech tec = db.tb_Tech.FirstOrDefault(t => t.TechId == item.TechId);
                    user.tb_Tech.Add(tec);
                }
                if (!user.tb_Power.IsLoaded)
                {
                    user.tb_Power.Load();
                }
                user.tb_Power.Clear();
                foreach (tb_Power power in powerList)
                {
                    tb_Power po = db.tb_Power.FirstOrDefault(p=>p.PowerId==power.PowerId);
                    user.tb_Power.Add(po);
                }
                user.tb_WorkerGroup = groupObj;
                user.LoginName = obj.LoginName;
                user.LoginPwd = obj.LoginPwd;
                user.HeadPicPath = obj.HeadPicPath;
                user.IsLeader = obj.IsLeader;
                user.IsUseful = obj.IsUseful;
                user.MaxTaskCount = obj.MaxTaskCount;
                user.UserName = obj.UserName;
                db.SaveChanges();
                return 1;
            }
        }
        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="obj"></param>
        public static void DeleteUser(users obj)
        {
            users user = DataEntites.users.First(u => u.userID == obj.userID);
            DataEntites.DeleteObject(user);
            DataEntites.SaveChanges();
        }

    }
}
